/**
 * Package:com.goldcitynet.flex;
 * $Id: Util.java,v 1.7 2009/04/24 01:41:07 xuh Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.util;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xpath.XPathAPI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.dom4j.Document;
import org.dom4j.DocumentException;

import javax.xml.transform.TransformerException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;


/**
 * Util
 * <p/>
 * <p><a href="Util.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: xuh $</a>
 * @version $Revision: 1.7 $
 */
public class XMLUtil {
    private static Log log = LogFactory.getLog(XMLUtil.class);
    /**
     * 返回某节点的�?
     *
     * @param node 节点对象
     * @param xpath
     * @return 该节点的�?
     */
    public static String value(Node node, String xpath) {
        Node n=null;
        try {
            n= XPathAPI.selectSingleNode(node, xpath);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        }
        if(n==null || n.getFirstChild()==null)return null;
        return n.getFirstChild().getNodeValue();
    }
    /**
     * 返回某节点的值列�?只支持一�?br/>
     * �?
     *
     * @param node 节点对象
     * @param xpath
     * @return 该节点的子节点�?列表
     */
    public static List<String> values(Node node, String xpath) {
        List<String> list = new ArrayList<String>();
        NodeList n=null;
        try {
            n= XPathAPI.selectNodeList(node, xpath);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        }
        for(int i=0; i<n.getLength();i++){
//            log.debug(value(n.item(i),"id"));
//            log.debug(value(n.item(i),"name"));

            if(n.item(i)!=null) list.add(n.item(i).getFirstChild().getNodeValue());
        }
        return list;
    }

      /**
     * 返回某节点的列表
     *
     * @param node 某节点对�?
     * @param xpath
     * @return 节点列表
     */
    public static List<Node> nodes(Node node, String xpath) {
        List<Node> nodes  = new ArrayList<Node>();
         NodeList values = null;
        try {
            values =  XPathAPI.selectNodeList(node, xpath);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        }
        for(int i=0; i<values.getLength();i++){
            nodes.add(values.item(i));
        }
        return nodes;
    }


    /**
     * 转换Dom4j document 到w3cDocument
     * @param document
     * @return
     */
    public static org.w3c.dom.Document toW3cDocument(Document document){
       org.dom4j.io.DOMWriter d4Writer = new org.dom4j.io.DOMWriter();
        try
            {
                return d4Writer.write(document);
            }
            catch(DocumentException e)
            {
                log.debug("can't cast dom4jDocument to W3CDocument", e);
                return null;
            }

    }

    public static void main(String[] args) throws IOException, SAXException {
        String xml = "<package>\n" +
                "  <item>\n" +
                "    <packageid>402880481ec9cb4a011ec9cd207f0003</packageid>\n" +
                "    <position>10</position>\n" +
                "  </item>\n" +
                "  <item>\n" +
                "    <packageid>402880481ec9d4ff011ec9d636cb0003</packageid>\n" +
                "    <position>17</position>\n" +
                "  </item>\n" +
                "  <item>\n" +
                "    <packageid>402880481ec9d4ff011ec9d693330006</packageid>\n" +
                "    <position>16</position>\n" +
                "  </item>\n" +
                "  <action>savePosition</action>\n" +
                "  <id>10004</id>\n" +
                "</package>";
        DOMParser parser = new DOMParser();
        parser.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))));
        Element root = parser.getDocument().getDocumentElement();
        System.out.println(":"+ XMLUtil.value(root, "/package/action"));
        List<Node> nodes = nodes(root,"/package/item");
        for (Node node : nodes) {
            System.out.println(XMLUtil.value(node,"packageid"));
            System.out.println(XMLUtil.value(node,"position"));
        }

    }

}